/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.zhouwei.mzbanner.sample.slice;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.zhouwei.mzbanner.MZBannerView;
import com.zhouwei.mzbanner.holder.MZHolderCreator;
import com.zhouwei.mzbanner.holder.MZViewHolder;
import com.zhouwei.mzbanner.sample.ResourceTable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * NetImagesAbilitySlice
 */
public class NetImagesAbilitySlice extends AbilitySlice {

    private MZBannerView mMZBanner;
    private MZBannerView mNormalBanner;
    private static String []imagesMZ={
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi0.hdslb.com%2Fbfs%2Farticle%2F725c03f7f1c7346291e8a4b7187e2b7ad762fd3c.jpg&refer=http%3A%2F%2Fi0.hdslb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=22500bdcab3b90b7a7ee38330acecb34",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.wallcoo.com%2Fnature%2F2009_Landscape_1680_Desktop_03%2Fwallpapers%2F1280x1024%2FAgricultural%2520Fields%2520and%2520Village%2520Tibet.jpg&refer=http%3A%2F%2Fwww.wallcoo.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=38ec6713b22d2f9a903da65d608a51e6",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg2.3lian.com%2F2014%2Ff5%2F11%2Fd%2F51.jpg&refer=http%3A%2F%2Fimg2.3lian.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=3620fb9390585ac6d01e8fc4821bd207",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimage.biaobaiju.com%2Fuploads%2F20190623%2F22%2F1561298984-bAuNFdmJok.jpg&refer=http%3A%2F%2Fimage.biaobaiju.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=434ac89dac1e857e907cd391720d3945",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.wallcoo.com%2Fnature%2F2009_Landscape_1680_Desktop_03%2Fwallpapers%2F1680x1050%2FWild%2520Goose%2520Island%2520on%2520Saint%2520Mary%2520Lake%2520at%2520Sunrise%2520Glacier%2520National%2520Park%2520Montana.jpg&refer=http%3A%2F%2Fwww.wallcoo.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=77b1b665fbd7ebb89fca615ef87ef68d",
            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.pconline.com.cn%2Fimages%2Fupload%2Fupc%2Ftx%2Fphotoblog%2F1206%2F13%2Fc12%2F11988648_11988648_1339594072859.jpg&refer=http%3A%2F%2Fimg.pconline.com.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1613116329&t=f3c9bc53d5acb774cb2dccde74390d77",

    };


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_net_main);
        mMZBanner = (MZBannerView)findComponentById(ResourceTable.Id_banner);
        mMZBanner.setBannerPageClickListener((view, position) -> {
        });
        mMZBanner.addPageChangeListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {
            }

            @Override
            public void onPageSlideStateChanged(int i) {
            }

            @Override
            public void onPageChosen(int i) {
            }
        });
        List<String> nomalbannerlist = new ArrayList<>();
        for(int i=0;i<imagesMZ.length;i++){
            nomalbannerlist.add(imagesMZ[i]);
        }

        List<String> mzbannerList = new ArrayList<>();
        for(int i=0;i<imagesMZ.length;i++){
            mzbannerList.add(imagesMZ[i]);
        }
        mMZBanner.setIndicatorVisible(true);
        mMZBanner.setPages(mzbannerList, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });

        mNormalBanner = (MZBannerView)findComponentById(ResourceTable.Id_banner_normal);
        mNormalBanner.setPages(nomalbannerlist, new MZHolderCreator<BannerViewHolder>() {
            @Override
            public BannerViewHolder createViewHolder() {
                return new BannerViewHolder();
            }
        });
        mMZBanner.start();
        mNormalBanner.start();
    }



    private static class BannerViewHolder implements MZViewHolder<String> {
        private Image mImageView;
        private DisplayImageOptions options;
        @Override
        public Component createView(Context context) {
            // 返回页面布局文件
            Component view = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_banner_item,null,false);
            mImageView = (Image) view.findComponentById(ResourceTable.Id_banner_image);
            options = new DisplayImageOptions.Builder()
                    .showImageOnLoading(ResourceTable.Media_icon)
                    .showImageForEmptyUri(ResourceTable.Media_icon)
                    .showImageOnFail(ResourceTable.Media_icon)
                    .cacheInMemory(true)
                    .cacheOnDisk(true)
                    .considerExifParams(true)
                    .bitmapConfig(PixelFormat.RGB_565)
                    .build();
            return view;
        }

        @Override
        public void onBind(Context context, int position, String data) {
            ImageLoader.getInstance().displayImage(data,mImageView,options,new SimpleImageLoadingListener(){
                @Override
                public void onLoadingComplete(String imageUri, Component view, PixelMap loadedImage) {
                    super.onLoadingComplete(imageUri, view, loadedImage);
                }
            });
        }
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);

    }
}
